/**
 * 
 */
package edu.arizona.eller.mis.hippo.adapter.impl;

import java.util.ArrayList;
import java.util.List;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.arizona.eller.mis.hippo.adapter.ProductSearch;
import edu.arizona.eller.mis.hippo.bean.Product;
import edu.arizona.eller.mis.hippo.exception.SimpleException;
import edu.arizona.eller.mis.hippo.facade.AmazonFacade;

/**
 * @author Blaine Boule - blaineb@email.arizona.edu
 *
 */
public class AmazonAdapter implements ProductSearch {

	/* (non-Javadoc)
	 * @see edu.arizona.eller.mis.hippo.adapter.ProductSearch#keywordSearch(java.lang.String)
	 */
	@Override
	public List<Product> keywordSearch(String keyword) throws SimpleException {
		List<Product> products = new ArrayList<Product>();
		AmazonFacade af = new AmazonFacade();
		Document result;
		try {
			result = af.fullSearch(keyword);
			products = convertToProduct(result);
		} catch (Throwable e) {
			e.printStackTrace();
			throw new SimpleException("Exception thrown in Amazon Adapter");
		}
		
		return products;
	}

	/* (non-Javadoc)
	 * @see edu.arizona.eller.mis.hippo.adapter.ProductSearch#keywordSearch(java.lang.String, java.lang.String)
	 */
	@Override
	public List<Product> keywordSearch(String keyword, String condition)
			throws SimpleException {
		List<Product> products = new ArrayList<Product>();
		AmazonFacade af = new AmazonFacade();
		Document result;
		try {
			result = af.conditionSearch(keyword, condition);
			products = convertToProduct(result);
		} catch (Throwable e) {
			e.printStackTrace();
			throw new SimpleException("Exception thrown in Amazon Adapter");
		}
		
		return products;
	}

	/* (non-Javadoc)
	 * @see edu.arizona.eller.mis.hippo.adapter.ProductSearch#titleSearch(java.lang.String)
	 */
	@Override
	public List<Product> titleSearch(String keyword) throws SimpleException {
		List<Product> products = new ArrayList<Product>();
		AmazonFacade af = new AmazonFacade();
		Document result;
		try {
			result = af.fullSearch(keyword);
			products = convertToProduct(result);
		} catch (Throwable e) {
			e.printStackTrace();
			throw new SimpleException("Exception thrown in Amazon Adapter");
		}
		
		return products;
	}

	/* (non-Javadoc)
	 * @see edu.arizona.eller.mis.hippo.adapter.ProductSearch#titleSearch(java.lang.String, java.lang.String)
	 */
	@Override
	public List<Product> titleSearch(String keyword, String condition)
			throws SimpleException {
		List<Product> products = new ArrayList<Product>();
		AmazonFacade af = new AmazonFacade();
		Document result;
		try {
			result = af.conditionSearch(keyword, condition);
			products = convertToProduct(result);
		} catch (Throwable e) {
			e.printStackTrace();
			throw new SimpleException("Exception thrown in Amazon Adapter");
		}
		
		return products;
	}
	
	private List<Product> convertToProduct(Document doc) throws SimpleException{
		List<Product> products = new ArrayList<Product>();
		try {
			if (doc != null) {
				XPath xpath = XPathFactory.newInstance().newXPath();
				XPathExpression itemExpression;
				
				itemExpression = xpath.compile("//Item");
				
				NodeList nodes = (NodeList) itemExpression.evaluate(doc,XPathConstants.NODESET);
				Product p;
				for (int i = 0; i < nodes.getLength(); i++) {
					p = new Product();
					Node node = nodes.item(i);
	
					p.setId((String) xpath.evaluate("ASIN", node,XPathConstants.STRING));
					p.setName((String)xpath.evaluate("ItemAttributes/Title", node,XPathConstants.STRING));
					NodeList features = (NodeList)xpath.evaluate("ItemAttributes/Feature", node,XPathConstants.NODESET);
					String description = "";
					for(int j=0;j<features.getLength();j++){
						Node feature = features.item(j);
						description += " | " + feature.getTextContent();
					}
					
					
					p.setDescription(description);
					p.setUrl((String) xpath.evaluate("ItemAttributes/DetailPageURL", node,XPathConstants.STRING));
					p.setPrice((String) xpath.evaluate("ListPrice/FormattedPrice", node,XPathConstants.STRING));
					products.add(p);
				}
			}
		} catch (XPathExpressionException e) {
			e.printStackTrace();
			throw new SimpleException("XPathExpression Exception durring product conversion");
		}

		return products;
	}

}
